package com.xinqi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinqi.entity.User;
import com.xinqi.mapper.UserMapper;
import com.xinqi.service.UserService;
import com.xinqi.utils.SqlSessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xin麒
 * @date 2023/6/25 15:17
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public List<User> getPageByLimitationsAndPages(Page<User> pageParam) {
        Page<User> userPage = baseMapper.selectPage(pageParam,null);
        return userPage.getRecords();
    }

    @Override
    public int countAll() {
//        return baseMapper.selectCount();
        SqlSession sqlSession = SqlSessionUtil.getSqlSessionWithAutoCommit();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.selectCount();
        sqlSession.close();
        log.debug("i is {}",i);
        return i;
    }

    @Override
    public int insertUser() {
//        return baseMapper.selectCount();
        SqlSession sqlSession = SqlSessionUtil.getSqlSessionWithAutoCommit();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.insertUser();
        sqlSession.close();
        log.debug("result is {}",result);
        return result;
    }

    @Override
    public int insertUserWithNoAutoCommit() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSessionWithNoAutoCommit();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.insertUser();
        sqlSession.commit();
        mapper.insertUser();
        sqlSession.commit();
        sqlSession.close();
        log.debug("result is {}",result);
        return result;
    }



}
